package com.joesem.sale.client.widget.dialog

import android.app.Application
import android.os.Bundle
import androidx.fragment.app.FragmentManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.DialogWorkOderParamsBinding
import com.joesem.sale.client.base.*
import com.joesem.sale.client.network.data.CustomerParamsMode
import com.joesem.sale.client.network.data.DeptMode
import com.joesem.sale.client.utils.click

const val TYPE_TYPES = "types"
const val TYPE_INDUSTRIES = "industries"
const val TYPE_PROPERTIES = "properties"
const val TYPE_TAGS = "tags"
const val TYPE_LEVELS = "levels"
const val TYPE_SOURCES = "sources"
const val TYPE_NETWORK_POINT = "NETWORK_POINT"

class CustomerParamsDialog(
    val title: String,
    val type: String = "",
    val selected: (name: String, code: String) -> Unit
) :
    BaseBottomFragmentDialog<DialogWorkOderParamsBinding>() {
    private val adapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_single_text) {
            var itemClick: (String, Int) -> Unit = { item, position -> }
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.setText(R.id.tv_title, item)
                holder.itemView.click {
                    itemClick.invoke(item, holder.layoutPosition)
                }
            }
        }
    private val viewModel by createViewModel<CustomerParamsViewModel>()
    private val deptData = arrayListOf<DeptMode>()
    override fun DialogWorkOderParamsBinding.initView(savedInstanceState: Bundle?) {
        refresh()
        rvParamsContent.adapter = adapter
        tvTitle.text = title
    }

    override fun DialogWorkOderParamsBinding.initListener() {
        btnClose.click {
            dismiss()
        }
        adapter.itemClick = { item, position ->
            if (type == TYPE_NETWORK_POINT) {
                selected.invoke(item, deptData[position].id.toString())
            } else {
                selected.invoke(item, item)
            }
            dismiss()
        }
    }

    private fun refresh() {
        bind.apply {
            llLoading.visible()
            pbLoading.visible()
            tvLoadingText.text = "加载中..."
            if (type == TYPE_NETWORK_POINT) {
                viewModel.dept()
            } else {
                viewModel.getParams()

            }
        }
    }

    override fun DialogWorkOderParamsBinding.initIntent(intent: Any?) {
        when (intent) {
            is CustomerParamsViewModel.ParamsIntent -> {
                if (intent.data != null) {
                    llLoading.gone()
                    when (type) {
                        TYPE_TYPES -> adapter.setList(intent.data.types?.filterNotNull())
                        TYPE_INDUSTRIES -> adapter.setList(intent.data.industries?.filterNotNull())
                        TYPE_PROPERTIES -> adapter.setList(intent.data.properties?.filterNotNull())
                        TYPE_TAGS -> adapter.setList(intent.data.tags?.filterNotNull())
                        TYPE_LEVELS -> adapter.setList(intent.data.levels?.filterNotNull())
                        TYPE_SOURCES -> adapter.setList(intent.data.sources?.filterNotNull())
                    }
                } else {
                    pbLoading.gone()
                    tvLoadingText.text = "加载失败，点击重试~"
                    tvLoadingText.click {
                        refresh()
                    }
                }
            }

            is CustomerParamsViewModel.ParamsNetworkPointIntent -> {
                if (intent.data == null) {
                    pbLoading.gone()
                    tvLoadingText.text = "加载失败，点击重试~"
                    tvLoadingText.click {
                        refresh()
                    }
                } else {
                    llLoading.gone()
                    deptData.clear()
                    deptData.addAll(intent.data)
                    adapter.setList(intent.data.map { it.name.orEmpty() })
                }
            }
        }
    }
}


class CustomerParamsViewModel(application: Application) : BaseViewModel(application) {
    fun getParams() {
        request(request = {
            customerParams()
        }, responseFailed = {
            toast(it.message)
            actionIntent(ParamsIntent(null))
        }, response = {
            actionIntent(ParamsIntent(it.data))
        })
    }

    fun dept() {
        request(request = {
            getDept()
        }, responseFailed = {
            toast(it.message)
            actionIntent(ParamsNetworkPointIntent(null))
        }, response = {
            actionIntent(ParamsNetworkPointIntent(it.data?.filterNotNull() ?: arrayListOf()))
        })
    }

    class ParamsIntent(val data: CustomerParamsMode?)
    class ParamsNetworkPointIntent(val data: List<DeptMode>?)
}

fun optionCustomerParams(
    fm: FragmentManager,
    title: String,
    type: String,
    result: (name: String, code: String) -> Unit
) {
    CustomerParamsDialog(title = title, type = type, selected = result).show(fm, null)
}
